package Gui;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;

import Main.Defines;
import PlayerManager.Player;

public class BottomStatsPanel extends JPanel {

	ArrayList<JLabel> m_playersCashLoanList;
	JLabel m_jackpot;
	// loan amount
	private static BottomStatsPanel ref;
	private void initLabels(){
		m_playersCashLoanList = new ArrayList<JLabel>();
		for (Player a : Defines.PLM.getPlayerList()) {
			JLabel thisPlayersLabel = new JLabel();
			String labelText = "";
			labelText += "<html>Player " + a.getIDPrintable();
			labelText += "<br> Cash: $" + a.getCash();
			labelText += "<br> Loan: $" + a.getLoan();
			thisPlayersLabel.setText(labelText);
			m_playersCashLoanList.add(thisPlayersLabel);
		}
		for (JLabel a : m_playersCashLoanList)
			add(a);
	}
	private BottomStatsPanel() {
		initLabels();
	
		m_jackpot = new JLabel("Jackpot: $" + Defines.GM.getLotteryPile());

		
		add(m_jackpot);
		setLayout(new GridLayout(1, m_playersCashLoanList.size(), 0, 0)); // magic number

	}
	public void resetInstance(){
		for(JLabel a : m_playersCashLoanList)
			remove(a);
		initLabels();
		m_jackpot.setText("Jackpot: $" + Defines.GM.getLotteryPile());
	}
	public static BottomStatsPanel getInstance() {
		if (ref == null) {
			ref = new BottomStatsPanel();
		}
		// //update();
		return ref;
	}

	public void update() {

		// JLabel m_playerNumber, m_moneyAmount, m_numDealCards, m_numMailCards,
		// m_loanAmount;
		// setLayout(new GridLayout(1, 5, 20, 10));
		for (int i = 0; i < m_playersCashLoanList.size(); ++i) {
			Player a = Defines.PLM.getPlayerList().get(i);
			
			String labelText = "";
			labelText += "<html>Player " + a.getIDPrintable();
			labelText += "<br> Cash: $" + a.getCash();
			labelText += "<br> Loan: $" + a.getLoan();
			m_playersCashLoanList.get(i).setText(labelText);
		}
		m_jackpot.setText("Jackpot: $" + Defines.GM.getLotteryPile());

	}

}
